<template>
  <div class="app-container-component">

    <div class="table-btn">
      <el-button type="primary" size="small" icon="el-icon-plus"
                 v-if="roleType === 0 || roleType === 5 || roleType === 9" @click="addTeam()">添加
      </el-button>

      <!--<el-button size="small" icon="el-icon-upload2">导出</el-button>-->
    </div>
    <my-table
      :data="tableData"
      :col-configs="colConfigs"
      v-loading="listLoading"
      :page-num="pageNum"
      :page-size="pageSize"
      @openVisible="openVisible"
    >
      <el-table-column
        slot="opt"
        label="操作"
        fixed="right"
        width="80"
        v-if="roleType === 0 || roleType === 5 || roleType === 9"
      >
        <template slot-scope="scope">
          <el-button
            @click.native.prevent="editTeam(scope.row.operationId)"
            type="text"
            size="small">
            修改
          </el-button>
          <el-popover
            placement="top"
            width="160"
            :ref="`popover-${scope.$index}`"
          >
            <p>确定删除这条数据吗?</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="$refs[`popover-${scope.$index}`].doClose()">取消</el-button>
              <el-button type="primary" size="mini" @click.native.prevent="deleteRow(scope)">确定</el-button>
            </div>
            <el-button type="text" size="small" slot="reference" v-popover:popover-${scope.$index}>删除</el-button>
          </el-popover>
        </template>
      </el-table-column>
    </my-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNum"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    <el-dialog :title="dialogTitle" :visible.sync="outerVisible" width="400">
      <el-form ref="teamRow" :rules="rules" :model="teamRow" label-width="100px">
        <el-form-item label="开票申请单" size="small">
          <input type="file" ref='file' @change="getFile($event)">
        </el-form-item>
        <el-form-item label="保证金申请单" size="small">
          <input type="file" ref='margin' @change="getMargin($event)">
        </el-form-item>
        <el-form-item label="原件申请单" size="small">
          <input type="file" ref='original' @change="getOriginal($event)">
        </el-form-item>
        <el-form-item label="四库平台模板" size="small">
          <input type="file" ref='templates' @change="getTemplates($event)">
        </el-form-item>
        <el-form-item label="出图名单申请" size="small">
          <input type="file" ref='draw' @change="getDraw($event)">
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="warning" size="small" @click="submitForm('teamRow')" v-no-more-click>确定</el-button>
        <el-button size="small" @click="outerVisible = false">取消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="imgMsg.label" :visible.sync="imgVisible" width="500px">
      <img class="img-dialog" :src="imgMsg.src" alt="">
    </el-dialog>
  </div>
</template>

<script>
  import { addOperation, deleteOperation, getList, updateOperation } from '@/api/operationfrom1'
  import myTable from '@/components/myTable'

  import { AreaSelect } from 'vue-area-linkage'
  import { pca } from 'area-data'

  const filePath = {
    props: ['colConfig'],
    methods: {
      openVisible(label, src) {
        const msg = {
          label: label,
          src: src
        }
        this.$emit('openImgVisible', msg)
      }
    },
    template: `
  <el-table-column :label="colConfig.label" :min-width="colConfig.minWidth">
      <span slot-scope="scope">
        <div v-if="colConfig.type==='img'">
          <a v-if="scope.row[colConfig.prop]" @click="openVisible(colConfig.label,scope.row[colConfig.prop])">查看
          </a>
        </div>
        <div v-else>
          <a v-if="scope.row[colConfig.prop]" target="_blank" :href="GLOBAL.filePathUrl+encodeURI(scope.row[colConfig.prop])">下载</a>
        </div>
      </span>
  </el-table-column>
  `
  }

  export default {
    components: {
      myTable,
      AreaSelect
    },
    data() {
      return {
        colConfigs: [
          { prop: 'billingPath', label: '开票申请单', minWidth: '20%', component: filePath },
          { prop: 'marginPath', label: '保证金申请单', minWidth: '20%', component: filePath },
          { prop: 'originalPath', label: '原件申请单', minWidth: '20%', component: filePath },
          { prop: 'templatePath', label: '四库平台模板', minWidth: '20%', component: filePath },
          { prop: 'drawingsPath', label: '出图名单申请', minWidth: '20%', component: filePath },
          { slot: 'opt' }
        ],
        searchForm: {},
        searchFormList: {},
        tenderNames: [],
        infoTitles: [],
        listLoading: true,
        dialogTitle: '新增资料',
        dialogType: 0,
        teamRow: {},
        teamRowClear: {},
        tableData: [],
        pageNum: 1,
        pageSize: 10,
        total: 1,
        outerVisible: false,
        innerVisible: false,
        pca: pca,
        rules: {},
        visible: false,
        imgVisible: false,
        imgMsg: {}
      }
    },
    created() {
      this.teamRowClear = JSON.parse(JSON.stringify(this.teamRow))
      this.fetchData()
    },
    computed: {
      roleType() {
        return this.$store.state.user.roleType
      }
    },
    methods: {
      fetchData() {
        this.searchFormList.pageNum = this.pageNum
        this.searchFormList.pageSize = this.pageSize
        this.listLoading = true
        console.log(this.searchFormList)
        getList(this.searchFormList).then(response => {
          this.tableData = response.responseBody.list
          this.total = response.responseBody.total
          this.listLoading = false
        })
      },
      search() {
        this.pageNum = 1
        this.searchFormList = JSON.parse(JSON.stringify(this.searchForm))
        this.fetchData()
      },
      fileClear() {
        if (this.$refs.file) {
          this.$refs.file.value = ''
        }
        if (this.$refs.margin) {
          this.$refs.margin.value = ''
        }
        if (this.$refs.original) {
          this.$refs.original.value = ''
        }
        if (this.$refs.templates) {
          this.$refs.templates.value = ''
        }
        if (this.$refs.draw) {
          this.$refs.draw.value = ''
        }
      },
      addTeam() {
        if (this.$refs['teamRow']) {
          this.$refs['teamRow'].resetFields()
        }
        this.outerVisible = true
        this.dialogTitle = '新增资料'
        this.teamRow = JSON.parse(JSON.stringify(this.teamRowClear))
        this.teamRow.isTender = 1
        this.dialogType = 1
        this.fileClear()
      },
      editTeam(operationId) {
        if (this.$refs['teamRow']) {
          this.$refs['teamRow'].resetFields()
        }
        this.outerVisible = true
        this.dialogTitle = '修改资料'
        this.dialogType = 0
        this.teamRow = JSON.parse(JSON.stringify(this.teamRowClear))
        this.fileClear()
        this.teamRow.operationId = operationId
      },
      submitForm(formName) {
        const params = new FormData()
        if (this.teamRow.file) {
          params.append('file', this.teamRow.file)
        }
        if (this.teamRow.margin) {
          params.append('margin', this.teamRow.margin)
        }
        if (this.teamRow.original) {
          params.append('original', this.teamRow.original)
        }
        if (this.teamRow.templates) {
          params.append('templates', this.teamRow.templates)
        }
        if (this.teamRow.draw) {
          params.append('draw', this.teamRow.draw)
        }
        if (!this.dialogType) {
          params.append('operationId', this.teamRow.operationId)
        }
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if (this.dialogType) {
              addOperation(params).then(response => {
                console.log(response)
                this.outerVisible = false
                this.fetchData()
              })
            } else {
              updateOperation(params).then(response => {
                console.log(response)
                this.outerVisible = false
                this.fetchData()
              })
            }
          } else {
            console.log('error submit!!')
            return false
          }
        })
      },
      deleteRow(scope) {
        this.$refs[`popover-${scope.$index}`].doClose()
        const params = new FormData()
        params.append('operationId', scope.row.operationId)
        params.append('isDel', 1)
        deleteOperation(params).then(response => {
          this.fetchData()
        })
      },
      handleSizeChange(val) {
        this.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.pageNum = val
        this.fetchData()
      },
      getFile(event) {
        this.teamRow.file = event.target.files[0]
      },
      getMargin(event) {
        this.teamRow.margin = event.target.files[0]
      },
      getOriginal(event) {
        this.teamRow.original = event.target.files[0]
      },
      getTemplates(event) {
        this.teamRow.templates = event.target.files[0]
      },
      getDraw(event) {
        this.teamRow.draw = event.target.files[0]
      },
      openVisible(msg) {
        this.imgVisible = true
        this.imgMsg = msg
      }
    },
    mounted() {
      console.log(this.pca)
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .app-container-component {
    height: auto;
    .el-pagination {
      margin-top: 20px;
    }
    .table-btn {
      margin-bottom: 10px;
      & > button {
        margin: 0 20px;
      }
      .el-dialog {
        textarea {
          margin-top: 10px;
        }
        .el-checkbox {
          margin-right: 20px;
        }
        .el-checkbox + .el-checkbox {
          margin-left: 0;
        }
      }
    }
    .item-inline {
      display: inline-block;
      width: 400px;
    }
    .img-dialog {
      max-width: 440px;
    }
  }

</style>


<style rel="stylesheet/scss" lang="scss">
  .el-input__icon {
    line-height: 100%;
  }

  .el-form-item__content {
    line-height: 33px;
  }

  .area-select .area-selected-trigger {
    padding: 0 10px;
  }

  a, a:focus, a:hover {
    color: #409EFF;
  }

  /*.el-table__body tr.hover-row>td {
    background-color: #0000ff !important;
  }

  .el-table__body tr.current-row>td {
    background-color: #0000ff !important;
  }*/
</style>
